"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 统一数据库管理工具
Case Name   : cm_ctl get,获取日志级别，一主多备数据库部署下cm_server的仲裁模式、AZ之间的切换模式
Create At   : 2022-07-07
Owner       : @daiguatutu
Description :
    1.获取cm日志级别
    2.获取cm_server的仲裁模式
    3.获取AZ之间的切换模式
Expect      :
    1.获取成功
    2.获取成功
    3.获取成功
History     : 
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

Pri_U_SH = CommonSH('PrimaryDbUser')
Node_Num = Pri_U_SH.get_node_num()


@unittest.skipIf(3 > Node_Num, '不满足一主两备环境跳过')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.dbuser_node = Node('dbuser')
        self.log.info(f"-----{os.path.basename(__file__)} start-----")

    def test_server_tools1(self):
        text = '--step1:获取cm日志级别 expect:获取成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl get --log_level;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: log_level=WARNING', cmctl_cmd)
        self.assertIn('cm_ctl: gets it successfully.', cmctl_cmd)

        text = '--step2:获取cm_server的仲裁模式 expect:获取成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl get --cm_arbitration_mode;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: cm_arbitration_mode=MAJORITY', cmctl_cmd)
        self.assertIn('cm_ctl: gets it successfully.', cmctl_cmd)

        self.log.info('--step3:获取AZ之间的切换模式 expect:获取成功--')
        cmctl_cmd = f'''source {macro.DB_ENV_PATH};
                    cm_ctl get --cm_switchover_az_mode;'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: cm_switchover_az_mode=AUTO', cmctl_cmd)
        self.assertIn('cm_ctl: gets it successfully.', cmctl_cmd)

    def tearDown(self):
        self.log.info('--无需清理环境--')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
